//package com.zhouyu.exampleshiro.config;
//
//import com.alibaba.fastjson.JSONObject;
//import com.zhouyu.exampleshiro.service.UserService;
//import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.authc.AuthenticationException;
//import org.apache.shiro.authc.AuthenticationInfo;
//import org.apache.shiro.authc.AuthenticationToken;
//import org.apache.shiro.authc.SimpleAuthenticationInfo;
//import org.apache.shiro.authz.AuthorizationInfo;
//import org.apache.shiro.authz.SimpleAuthorizationInfo;
//import org.apache.shiro.realm.AuthorizingRealm;
//import org.apache.shiro.subject.PrincipalCollection;
//import org.apache.shiro.subject.Subject;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//
///**
// * 在这里实现用户的验证和授权
// */
//public class JwtAuriRealm extends AuthorizingRealm {
//
//    private static final Logger LOGGER = LoggerFactory.getLogger(AuthorizingRealm.class);
//
//    @Autowired
//    UserService userService;
//
//    /**
//     * 授权realm
//     *
//     * @param principalCollection
//     * @return
//     */
//    @Override
//    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//        // 获取当前登陆用户
//        Subject subject = SecurityUtils.getSubject();
//        String userName = (String) subject.getPrincipal();
//        User user = userService.getUserByName(userName);
//        LOGGER.info("开始授权,user:{}", JSONObject.toJSONString(user));
//        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
//        if (user.getUsername().equals("root")) {
//            //超级管理员，拥有所有权限
//            authorizationInfo.addRole("*");
//            authorizationInfo.addStringPermission("*");
//        } else {
//            //为普通用户添加权限
//            for (Role role : user.getRoles()) {
//                authorizationInfo.addRole(role.getRoleName());
//                for (Permission permission : role.getPermissions()) {
//                    authorizationInfo.addStringPermission(permission.getName());
//                }
//            }
//        }
//        return authorizationInfo;
//    }
//
//    /**
//     * 认证
//     *
//     * @param authenticationToken
//     * @return
//     * @throws AuthenticationException
//     */
//    @Override
//    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//        LOGGER.info("开始认证");
//        //加这一步的目的是在Post请求的时候会先进认证，然后在到请求
//        if (authenticationToken.getPrincipal() == null) {
//            return null;
//        } else {
//            //获取用户信息
//            String name = authenticationToken.getPrincipal().toString();
//            User user = userService.getUserByName(name);
//            if (user == null) {
//                return null;
//            } else {
//                //这里验证authenticationToken和simpleAuthenticationInfo的信息
//                SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(name, user.getPassword(), getName());
//                return simpleAuthenticationInfo;
//            }
//        }
//    }
//}
